//
//  BusinessModel.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/4/14.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "BusinessModel.h"
#import "WHClient.h"
#import "PlaceEntity.h"
#import "OrderEntity.h"
#import "CompetEntity.h"
#import "Util.h"
#import "WarEntity.h"


@implementation BusinessModel{
    
    WHClient *_whClinet;
   
}

- (id)init {
    self = [super init];
    if (self) {
        _whClinet = [[WHClient alloc] init];
    }
    return self;
}


+ (BusinessModel *)sharedInstance{
    static BusinessModel *_sharedInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _sharedInstance = [[BusinessModel alloc] init];
    });
    
    return _sharedInstance;
}

- (void)getDataWithLatitude:(NSString *)latitude
                  longitude:(NSString *)longitude
                       page:(NSString *)page
               sportsItemId:(NSString *)sportsItemId
                     areaId:(NSString *)areaId
                       sort:(NSString *)sort
             sprotsHallName:(NSString *)sprotsHallName
               getPlaceList:(SuccessedInfoBlock)getPlaceList{

        NSString *vlatitude =[latitude copy];
        NSString *vlongitude =[longitude copy];
        NSString *vtype =[sportsItemId copy];
        NSString *varea =[areaId copy];
        NSString *vsort =[sort copy];
        NSString *vpage =[page copy];
        NSString *vname = [sprotsHallName copy];
    
    NSDictionary *data = @{@"latitude":vlatitude,@"longitude":vlongitude,
                           @"page":vpage,@"sportsItemId":vtype,
                           @"areaId":varea,
                           @"sort":vsort,@"sportsHallName":vname};
    [_whClinet GET:@"hhw-api/GetSportsHallList"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {

               NSMutableArray *tuArray = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   PlaceEntity *plaEty = [[PlaceEntity alloc] initWithAttributes:dic];
                    [tuArray addObject:plaEty];
                }
                getPlaceList(tuArray);
               
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"failed");
           }];
}

- (void)getMyOrderWithSatus:(NSString *)status
                       page:(NSString *)page
                      idNum:(NSString *)idNum
                 getMyOrder:(SuccessedInfoBlock)getMyOrder{
    NSString *mstatus = [status copy];
    NSString *mpage = [page copy];
    NSString *mid = [idNum copy];
    NSDictionary *data = @{@"status":mstatus,
                           @"page":mpage,
                           @"id":mid};
    [_whClinet GET:@"hhw-api/GetOrderList"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *array = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in responseObject[@"data"]) {
                   OrderEntity *ordEty = [[OrderEntity alloc] initWithAttributes:dic];
                   [array addObject:ordEty];
               }
               getMyOrder(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getMainListWithLatitude:(NSString *)latitude
                      longitude:(NSString *)longitude
                        getMain:(SuccessedInfoBlock)getMain{
    NSString *lat = [latitude copy];
    NSString *lon = [longitude copy];
    NSDictionary *data = @{@"latitude":lat,@"longitude":lon};
    [_whClinet GET:@"hhw-api/GetSportsHallRDList"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *tuArray = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   PlaceEntity *plaEty = [[PlaceEntity alloc] initWithAttributes:dic];
                   [tuArray addObject:plaEty];
               }
               getMain(tuArray);
               
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getPhoto:(SuccessedInfoBlock)getPhoto{
    
    [_whClinet GET:@"hhw-api/GetTopPic"
        parameters:nil
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSArray *photo = responseObject[@"data"];
               getPhoto(photo);
               
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getPlaceDateWithSprotsHallID:(NSString *)SprotsHallID
                            latitude:(NSString *)latitude
                           longitude:(NSString *)longitude
                        getPlaceData:(SuccessedInfoBlock)getPlaceData{
    NSString *spid = [SprotsHallID copy];
    NSString *splat = [latitude copy];
    NSString *splon = [longitude copy];
    NSDictionary *data = @{@"sportsHallId":spid,
                           @"latitude":splat,
                           @"longitude":splon};
    [_whClinet GET:@"hhw-api/GetSportsHall"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSArray *array = responseObject[@"data"];
               getPlaceData(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getplaceDataContWithsportsHallId:(NSString *)sportsHallId
                                    page:(NSString *)page
                                 getData:(SuccessedInfoBlock)getData{
    NSString *gsport = [sportsHallId copy];
    NSString *gpage = [page copy];
    NSDictionary *data = @{@"sportsHallId":gsport,
                           @"page":gpage};
    [_whClinet GET:@"hhw-api/GetEvaluation"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *data = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   PlaceEntity *placeEty = [[PlaceEntity alloc] initWithAttributes:dic];
                   [data addObject:placeEty];
               }
               getData(data);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getplaceDataHallWithsportsHallId:(NSString *)sportsHallId
                             getHallData:(SuccessedInfoBlock)getHallData{
    
    NSString *idNum = [sportsHallId copy];
    [_whClinet GET:@"hhw-api/GetSportsEvent"
        parameters:@{@"sportsHallid":idNum}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSArray *data = responseObject[@"data"];
               getHallData(data);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}


- (void)getCompetListWithPage:(NSString *)page
                    getCompet:(SuccessedInfoBlock)getCompet{
    NSString *gpage = [page copy];
    [_whClinet GET:@"hhw-api/GetSportsEventList"
        parameters:@{@"page":gpage}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *compet = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   CompetEntity *comEty = [[CompetEntity alloc] initWithAttributes:dic];
                   [compet addObject:comEty];
               }
               getCompet(compet);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
    
}


- (void)getDatawithpage:(NSString *)page
                   data:(SuccessedInfoBlock)data{
    NSString *apage =[page copy];
    [_whClinet GET:@"hhw-api/GetYueZhanInfoList"
        parameters:@{@"page":apage}
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSMutableArray *dataArray = [[NSMutableArray alloc] init];
               for (NSDictionary *dic in [responseObject objectForKey:@"data"]) {
                   WarEntity *warEty = [[WarEntity alloc] initWithAttributes:dic];
                   [dataArray addObject:warEty];
               }
               data(dataArray);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)getReservationWithSporthall:(NSString *)Sporthall
                      scheduledTime:(NSString *)scheduledTime
                          accountId:(NSString *)accountId
                         Sportevent:(NSString *)Sportevent
                         returnData:(SuccessedInfoBlock)returnData{
    NSString *sphid = [Sporthall copy];
    NSString *stime = [scheduledTime copy];
    NSString *accid = [accountId copy];
    NSString *speid = [Sportevent copy];
    NSDictionary *data = @{@"sportsHallId":sphid,@"scheduledTime":stime,
                           @"accountId":accid,@"sportsEventId":speid};
    [_whClinet GET:@"hhw-api/GetYardUnit"
        parameters:data
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               NSArray *array = [responseObject objectForKey:@"data"];
               returnData(array);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               NSLog(@"%@",error);
           }];
}

- (void)generateTheOrderWithYardId:(NSString *)yardId
                         accountId:(NSString *)accountId
                            status:(NSString *)status
                     sportsEventId:(NSString *)sportEventId
                     scheduledTime:(NSString *)scheduledTime
                        totalPrice:(NSString *)totalPrice
                         yardUnits:(NSArray *)yardUnits
                       resultBlcok:(SuccessedInfoBlock)block {
    NSDictionary *paras = @{@"yardId": yardId,
                            @"accountId": accountId,
                            @"status": status,
                            @"sportsEventId": sportEventId,
                            @"scheduledTime": scheduledTime,
                            @"totalPrice": totalPrice,
                            @"yardUnit": yardUnits};
    
    _whClinet.requestSerializer = [AFJSONRequestSerializer serializer];
    
    [_whClinet POST:@"hhw-api/SetOrder"
        parameters:paras
           success:^(AFHTTPRequestOperation *operation, id responseObject) {
               _whClinet.requestSerializer = [AFHTTPRequestSerializer serializer];
               
               NSDictionary *dict = responseObject;
               block(dict);
           } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
               _whClinet.requestSerializer = [AFHTTPRequestSerializer serializer];

               
               NSLog(@"message is %@", operation.responseString);
               NSLog(@"%@",error);
           }];
}


@end
